//Component Object
const app = getApp();
Component({
  properties: {
    imageList:{
      type:Array,
      value:[],
      observer: function(){}
    },
    isCanUpload:{
      type:Boolean,
      value:true,
      observer: function(){}
    },
    sourceType:{
      type:Boolean,
      value:['camera'],
      observer: function(){}
    },
  },
  

  

   
    methods: {
      preImg(e){
         let newArray=[];
         this.data.imageList.forEach(item => {
          newArray.push(item.url)
         });

        wx.previewImage({
          current: e.currentTarget.dataset.url, // 当前显示图片的链接/路径
          urls: newArray
        })
      },

      deleteImg(e){
        const temArray = this.data.imageList;
        temArray.splice(e.currentTarget.dataset.index,1);
        this.setData({
          imageList:temArray
        })
      },

      uploadImages: function (event) {
        const  that = this
        const temArray = this.data.imageList;
        wx.chooseMedia({
          count: 9, // 默认9  
          mediaType: ['image'],
          sourceType: this.properties.sourceType,
          camera: 'back', // 使用后置摄像头  
          success: function (res) {
            var path = res.tempFiles[0].tempFilePath;
            wx.showLoading({
              title: '图片上传中', // 提示的内容  
              mask: true, // 是否显示透明蒙层，防止触摸穿透  
            });
            wx.uploadFile({
              url: app.globalData.fileUrl + '/common/picUpload',
              filePath: path,
              name: 'file',
              success(res) {
                let fileInfo = JSON.parse(res.data);
                temArray.push({
                  url:`${app.globalData.fileUrl}/common/preview?image=${fileInfo.fileName}`,closeable:true,
                  fileNewName:fileInfo.fileName,
                  originalFileName:`${app.globalData.fileUrl}/common/preview?image=${fileInfo.fileName}`
                })
                that.setData({
                  imageList:temArray
                })
                wx.hideLoading();

                that.triggerEvent("upload",temArray);

              },
              fail(err) {
                wx.hideLoading();
                wx.showToast({
                  title: "上传失败",
                  icon: 'none',
                  duration: 3000
                });
              }
            });
          },
          fail(err) {
            wx.hideLoading();
            wx.showToast({
              title: "上传失败",
              icon: 'none',
              duration: 3000
            });
          }
        
        });
      },
      
    },
    lifetimes:{
      created: function(){

      },
      attached: function(){
  
      },
      ready: function(){
         //根据传入的值设置title
         console.log(1111, this.properties.isCanUpload)
   
      },
      moved: function(){
  
      },
      detached: function(){
  
      }
    }
    
});